<?php

namespace MVC\Index;

use  \Phalcon\Loader,
    \Phalcon\Mvc\ModuleDefinitionInterface,
    \ReflectionClass as RClass;

class Module implements ModuleDefinitionInterface
{
    private $moduleName;
    private $controllersDir;
    private $modelsDir;
    private $viewsDir;

    public function registerAutoloaders($di)
    {

        $class                = new RClass( $this );
        $moduleName           = $this->moduleName = strtolower( str_replace( 'MVC\\', null, $class->getNamespaceName() ) );
        $this->controllersDir = $di->getMvc()->$moduleName->controllersDir;
        $this->modelsDir      = $di->getMvc()->$moduleName->modelsDir;
        $this->viewsDir       = $di->getMvc()->$moduleName->viewsDir;

        $loader = new Loader();

        $loader->registerNamespaces( [
            ucfirst( $this->moduleName ) . '\Controllers' => ROOT_APP . $this->moduleName . DS . $this->controllersDir . DS,
            ucfirst( $this->moduleName ) . '\Models'      => ROOT_APP . $this->moduleName . DS . $this->modelsDir . DS,
        ] )->register();

    }

    public function registerServices( $di )
    {
        $di->getView()->setViewsDir( ROOT_APP . $this->moduleName . DS . $this->viewsDir );
    }

}